package com.capacitydorm.server.web;

import com.capacitydorm.server.common.vo.Result;
import com.capacitydorm.server.domain.Floor;
import com.capacitydorm.server.service.FloorService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/floor")
public class FloorController {

    @Resource
    private FloorService floorService;

    /**
     * 根据楼id查层
     * @param tid
     * @return
     */
    @GetMapping("/{tid}")
    public ResponseEntity<List<Floor>> selectFloorByTid(@PathVariable("tid") Integer tid) {
        try {
            List<Floor> list = floorService.selectAll(tid);
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 添加层
     */
    @PostMapping()
    public ResponseEntity<Result> addFloor(Floor floor){
        try{
            List<Floor> list =  floorService.check(floor);
            if (list.size() != 0){
                return  new ResponseEntity<>(new Result(500,"层重复"),HttpStatus.INTERNAL_SERVER_ERROR);
            }
            floorService.add(floor);
            return  new ResponseEntity<>(HttpStatus.CREATED);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
    /**
     * 删除层
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFloor(@PathVariable("id") Integer id){
        try {
            floorService.delete(id);
            return new ResponseEntity<>(HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
    /**
     *  修改层
     */
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateFloor(@PathVariable("id") Integer id,Floor floor){
        try{
            floorService.update(id,floor);
            return new ResponseEntity<>(HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}


